﻿using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Web.Base.Extensions;
using Web.Base.Security;
using Web.Seller.Models;
using System.Web.WebPages;
using System.Collections.Generic;
using Ayatta.Contract.Component;

namespace Web.Seller.Controllers
{
    public class ItemController : SellerBaseController
    {
        public ItemController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(int page = 1, int size = 20, byte param = 0, string keyword = null, string outerId = null, int? categoryRootId = null, decimal? priceFrom = null, decimal? priceTo = null, int? saleFrom = null, int? saleTo = null, string[] orderBy = null)
        {
            var id = keyword.AsInt(0);
            var identity = User.Identity.Identity();
            var name = id > 0 ? string.Empty : keyword;

            var model = new ItemControllerModel.Index();
            var dic = model.GetOrderByDic(orderBy);
            model.Param = param;
            model.Keyword = keyword;
            model.OuterId = outerId;
            model.CategoryRootId = categoryRootId;
            model.PriceFrom = priceFrom;
            model.PriceTo = priceTo;
            model.SaleFrom = saleFrom;
            model.SaleTo = saleTo;
            model.Items = StoreService.GetPagedProductItems(page, size,param, identity.Id, id, name, outerId, categoryRootId, priceFrom, priceTo, saleFrom, saleTo, dic).ToPagedList(page, size);

            return View(model);
        }

        public ActionResult Name(int param)
        {
            var model = StoreService.GetProductItemName(param);
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Name(int param, string name, string subName)
        {
            var identity = User.Identity.Identity();
            var result = new SkResult<string>();
            var status = result.Status = StoreService.UpdateProductItemName(param, identity.Id, name, subName);
            if (status)
            {
                result.Data = name;
                result.Extra = subName;
            }
            else
            {
                result.Message = "更新商品名称失败！";
            }
            return Json(result);
        }

        public ActionResult Price(int param)
        {
            var identity = User.Identity.Identity();
            var model = StoreService.GetProductItemPriceQuantity(param, identity.Id);
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Price(int param, int[] id, decimal[] price, decimal unitPrice)
        {
            var len = id.Length;
            var identity = User.Identity.Identity();
            var result = new SkResult<string>();

            var dic = new Dictionary<int, decimal>();
            for (var i = 0; i < len; i++)
            {
                var key = id[i];
                var value = price[i];
                dic.Add(key, value);
            }

            var min = dic.Min(o => o.Value);
            var max = dic.Max(o => o.Value);
            if (unitPrice >= min && unitPrice <= max)
            {
                var status = result.Status = StoreService.UpdateProductItemPrice(param, identity.Id, unitPrice, dic);
                if (status)
                {
                    result.Data = unitPrice.ToString("N");
                }
                else
                {
                    result.Message = "更新价格失败！";
                }
            }
            else
            {
                result.Message = "商品价格请设置在合理区间内！";
            }
            return Json(result);
        }

        public ActionResult Quantity(int param)
        {
            var identity = User.Identity.Identity();
            var model = StoreService.GetProductItemPriceQuantity(param, identity.Id);
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Quantity(int param, int[] id, int[] quantity)
        {
            var len = id.Length;
            var identity = User.Identity.Identity();
            var result = new SkResult<string>();

            var dic = new Dictionary<int, int>();
            for (var i = 0; i < len; i++)
            {
                var key = id[i];
                var value = quantity[i];
                dic.Add(key, value);
            }

            var total = dic.Sum(o => o.Value);
            result.Status = StoreService.UpdateProductItemQuantity(param, identity.Id, total, dic);
            var status = result.Status = StoreService.UpdateProductItemQuantity(param, identity.Id, total, dic);
            if (status)
            {
                result.Data = total.ToString();
            }
            else
            {
                result.Message = "更新库存失败！";
            }
            return Json(result);
        }
    }
}
